'use strict';

const Service = require('egg').Service;

const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require("../oltmodules/HuaWeiOLT")
const XDOLT = require("../oltmodules/XinDeOLT")
const XGLOLT = require("../oltmodules/XinGeLinOLT")
const ulid = require("../public/ulid")
class UplinkService extends Service {
   async list(params) {
    let result = {
      count:"",
      rows:""
    }
    result.count = await this.ctx.model.UplinkPorts.find({oltId:params.OltId}).count('_id')
    result.rows = await this.ctx.model.UplinkPorts.find({oltId:params.OltId}).sort('p').sort('s').skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async configRefresh(params){
    const uplinkInformation = await this.ctx.model.Uplink.findOne({where:{id:params.id}})
    const oltInformation = await this.ctx.model.Olt.findOne({where:{id:uplinkInformation.oltId}})
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.snmpReadOnlyCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result = {
      msg:null,
      data:null
    }
    try {
      switch (oltInformation.oltType) {
        case "ZTE":
          //查询状态 查询tag和untag
          const zte = new ZTEOLT(options)
          var temp = await zte.getUplinkAdminStatusAndVlan(uplinkInformation.PortID,oltInformation.oltHardVersion,this)
          break;
        case "HUAWEI":
          const hw = new HWOLT(options)
          var temp = await hw.getUplinkAdminStatusAndVlan(uplinkInformation.PortID,oltInformation.oltHardVersion,this)
          break;
        case "OPTICTIMES":
          const xd = new XDOLT(options)
          var res = await xd.getUplinkAdminstatusAndUnTagVlan(uplinkInformation.PortID)
          var temp = await xd.getUplinkConfig(res,uplinkInformation.PortID,oltInformation.oltHardVersion,this)
          break;
        case "XINGELIN":
          const xgl = new XGLOLT(options)
          var temp = await xgl.getUplinkAdminStatusAndVlan(uplinkInformation.PortID,oltInformation.oltHardVersion,this)
          break;
      }
      result.data = temp
      result.msg = true
      return result
    } catch (error) {
      result.msg = false
      return result
    }
  }
  // async editUnplik(params) {
  //   const uplinkInformation = await this.ctx.model.Uplink.findOne({where:{id:params.id}})
  //   const oltInformation = await this.ctx.model.Olt.findOne({where:{id:uplinkInformation.oltId}})
  //   const options = new ConnectOption(oltInformation.oltIP, oltInformation.snmpReadOnlyCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName);
  //   switch (oltInformation.oltType) {
  //     case "ZTE":
  //       const zte = new ZTEOLT(options)
  //       await zte.setUplink(uplinkInformation.PortID,params.adminStatus,params.addvlan,params.UnTagvlan,params.oldUnTagvlan,params.removevlan,oltInformation.oltHardVersion,this)
  //       break;
  //     case "HUAWEI":
  //       const hw = new HWOLT(options)
  //       await hw.setUplink(uplinkInformation.PortID,params.adminStatus,params.addvlan,params.UnTagvlan,params.oldUnTagvlan,params.removevlan,oltInformation.oltHardVersion,this)
  //       break;
  //     case "OPTICTIMES":
  //       const xd = new XDOLT(options)
  //       await xd.setUplink(uplinkInformation.PortID,params.adminStatus,params.addvlan,params.UnTagvlan,params.oldUnTagvlan,params.removevlan,oltInformation.oltHardVersion,this)
  //       break;
  //     case "XINGELIN":
  //       const xgl = new XGLOLT(options)
  //       await xgl.setUplink(uplinkInformation.PortID,params.adminStatus,params.addvlan,params.UnTagvlan,params.oldUnTagvlan,params.removevlan,oltInformation.oltHardVersion,this)
  //       break;
  //     }
  // }
  // async uplinkCreateVlan(params) {
  //   const result = await this.ctx.model.Vlan.findAll({where:{oltId:params.oltId}})
  //   const vlanArr = []
  //   result.forEach(element => {
  //     vlanArr.push(element.VlanID)
  //   }); 
  //   //遍历数组 不存在的加库
  //   if(params.arr.length != 0){
  //     let arrlist = this.arrayRepeat(vlanArr,params.arr)
  //     arrlist.forEach(async (item) =>{
  //       await this.ctx.model.Vlan.create({
  //         id:ulid.ulid(),
  //         tid:this.ctx.session.admin.id,
  //         oltId:params.oltId,
  //         VlanID:item,
  //       })
  //     })
  //   }
  // }
  arrayRepeat (array1, array2) {
    var result = []
    for (var i = 0; i < array2.length; i++) {
      var obj = array2[i]
      var num = obj
      var isExist = false
      for (var j = 0; j < array1.length; j++) {
        var aj = array1[j]
        var n = aj
        if (n === num) {
          isExist = true
          break
        }
      }
      if (!isExist) {
        result.push(obj)
      }
    }
    return result
  }
  async getUplinkMsg(params){
    let result = {
      msg:"",
      data:"",
      flag:false
    }
    let uplinkResult = await this.ctx.model.UplinkPorts.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:uplinkResult.oltId})
    try {
      let info = await getUplinkMsg(uplinkResult,oltResult,this)
      result.msg = "getPortMsg success",
      result.data = info
      result.flag = true
      return result
    } catch (error) {
      result.msg = "getPortMsg fail,"+error
      result.flag = false
      result.data = ""
      return result
    }
  }
  async setUplinkMsg(params){
    let result = {
      msg:"",
      flag:false
    }
    let uplinkResult = await this.ctx.model.UplinkPorts.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltID})
    try {
      await setUplinkMsg(uplinkResult,params.tagVlan,params.removeTagVlan,params.type,params.unTagVlan,oltResult,this)
      await this.ctx.model.UplinkPorts.updateOne({_id:params.id},{$set:{adminStatus:params.type}})
      if(params.tagVlan){
        let tagVlanResult = await this.ctx.model.Vlan.findOne({VlanID:params.tagVlan})
        if(tagVlanResult == null){
          await this.ctx.model.Vlan.create({_id:ulid.ulid(),oltId:params.oltID,VlanID:params.tagVlan,description:""})
        }
      }
      if(params.unTagVlan){
        let unTagVlanResult = await this.ctx.model.Vlan.findOne({VlanID:params.unTagVlan})
        if(unTagVlanResult == null){
          await this.ctx.model.Vlan.create({_id:ulid.ulid(),oltId:params.oltID,VlanID:params.unTagVlan,description:""})
        }
      }
      result.msg = "setUplinkMsg successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "setUplinkMsg fail,"+error
      result.flag = false
      return result
    }
  }
}
async function getUplinkMsg(uplinkResult,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.getUplinkMsg(uplinkResult,oltInformation.oltHardVersion,me)
        break;
        case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.getUplinkMsg(uplinkResult,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function setUplinkMsg(uplinkResult,tagVlan,removeTagVlan,type,unTagVlan,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    let cmd
    switch (oltInformation.oltType) {
      case "ZTE":
        if(type == 'up'){
          cmd = 'no shutdown'
        }else if(type == 'down'){
          cmd = 'shutdown'
        }
        const zte = new ZTEOLT(options);
        result = await zte.setUplinkMsg(uplinkResult,tagVlan,removeTagVlan,cmd,unTagVlan,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        if(type == 'up'){
          cmd = 'undo shutdown'
        }else if(type == 'down'){
          cmd = 'shutdown'
        }
        const hw = new HWOLT(options)
        result = await hw.setUplinkMsg(uplinkResult,tagVlan,removeTagVlan,cmd,unTagVlan,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
module.exports = UplinkService;
